package com.py.credit.utils;

import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.LinkedList;

public class FormulaCalculator
{
	private static boolean isRightFormat = true;  
	   
    public static String getResult(String formula){    
        String returnValue = "0";    
        try{    
            returnValue = String.valueOf(doAnalysis(formula));    
        }catch(NumberFormatException nfe){     
        	returnValue = "公式格式有误，请检查"+ formula;
            System.out.println("公式格式有误，请检查:" + formula);    
        }catch(Exception e){     
            e.printStackTrace();    
        }   
        if(!isRightFormat){    
            System.out.println("公式格式有误，请检查:" + formula);    
        }   
        return returnValue;   
    }  
   
    private static double doAnalysis(String formula){  
        double returnValue = 0;    
        LinkedList<Integer> stack = new LinkedList<Integer>();    
        int curPos = 0;    
        String beforePart = "";   
        String afterPart = "";    
        String calculator = "";   
        isRightFormat = true;   
        while(isRightFormat&&(formula.indexOf('(') >= 0||formula.indexOf(')') >= 0)){           
            curPos = 0;    
            for(char s : formula.toCharArray()){     
                if(s == '('){       
                    stack.add(curPos);     
                }else if(s == ')'){       
                    if(stack.size() > 0){       
                        beforePart = formula.substring(0, stack.getLast());       
                        afterPart = formula.substring(curPos + 1);       
                        calculator = formula.substring(stack.getLast() + 1, curPos);        
                        formula = beforePart + doCalculation(calculator) + afterPart;       
                        stack.clear();       
                        break;       
                    }else{        
                        System.out.println("有未关闭的右括号！");       
                        isRightFormat = false;       
                    }     
                }      
                curPos++;     
            }     
            if(stack.size() > 0){      
                System.out.println("有未关闭的左括号！");     
                break;     
            }   
        }    
        if(isRightFormat){    
            returnValue = doCalculation(formula);   
        }    
        return returnValue;   
    }  
      
    private static double doCalculation(String formula) {    
        ArrayList<Double> values = new ArrayList<Double>();    
        ArrayList<String> operators = new ArrayList<String>();   
        int curPos = 0;   
        int prePos = 0;  
        int minus = 0;        
        for (char s : formula.toCharArray()) {   
             if ((s == '+' || s == '-' || s == '*' || s == '/') && minus !=0 && minus !=2) {                                               
                 values.add(Double.parseDouble(formula.substring(prePos, curPos).trim()));                    
                 operators.add("" + s);                   
                 prePos = curPos + 1;                  
                 minus = minus +1;  
             }else{                
                  minus =1;                
             }  
             curPos++;        
        }    
        values.add(Double.parseDouble(formula.substring(prePos).trim()));   
        char op;    
        for (curPos = 0; curPos <= operators.size() - 1; curPos++) {                           
            op = operators.get(curPos).charAt(0);     
            switch (op) {    
            case '*':      
                values.add(curPos, values.get(curPos) * values.get(curPos + 1));     
                values.remove(curPos + 1);      
                values.remove(curPos + 1);     
                operators.remove(curPos);    
                curPos = -1;  
                break;     
            case '/':     
                values.add(curPos, values.get(curPos) / values.get(curPos + 1));      
                values.remove(curPos + 1);      
                values.remove(curPos + 1);      
                operators.remove(curPos);    
                curPos = -1;  
                break;     
            }    
        }    
        for (curPos = 0; curPos <= operators.size() - 1; curPos++) {     
            op = operators.get(curPos).charAt(0);    
            switch (op) {    
            case '+':      
                values.add(curPos, values.get(curPos) + values.get(curPos + 1));      
                values.remove(curPos + 1);      
                values.remove(curPos + 1);     
                operators.remove(curPos);    
                curPos = -1;  
                break;   
            case '-':      
                values.add(curPos, values.get(curPos) - values.get(curPos + 1));     
                values.remove(curPos + 1);   
                values.remove(curPos + 1);     
                operators.remove(curPos);    
                curPos = -1;  
                break;    
            }   
        }    
        return values.get(0).doubleValue();  
    }  
   
    public static void main(String[] args) {    
    	DecimalFormat df = new DecimalFormat("#0.00"); 
    	/*String s = "500";
    	String x = "({0}/1000*90+10)/100*0.26";
    	String cc = MessageFormat.format(x, s);
    	System.out.println(cc);
        System.out.println(df.format(AAFormula.getResult(cc)));*/
    	//(({0}-0.01)/0.29*90+10)/100*2.55
    	String s = "2%";
    	if (s.indexOf("%") > 0 ){
    		s = s.substring(0, s.indexOf("%"));
    	}
    	float qq = Float.valueOf(s)/100;
    	String x = "(({0}-0.01)/0.29*90+10)/100*2.55";
    	String cc = MessageFormat.format(x, qq);
    	System.out.println(cc);
        System.out.println(FormulaCalculator.getResult(cc));
        System.out.println(df.format(FormulaCalculator.getResult(cc)));
    }  
}
